new Vue({
  el: '#app',
  data() {
    return {
      form: {
        page: 1,			//页数
        limit: 10,
        serachKey:''			//每页数据条数
      },
      formData: {},
      tableData: [{}],		//表格数据
      total: 1,
      dialogForm: false,
      title: '',
      multipleSelection: []
    }
  },
  mounted() {
    this.getList({
      pageNow: this.form.page,
      pageSize: this.form.limit,
      Gift_Card_Scheme_Name: ''
    })
  },
  methods: {
    handleSizeChange(val) {
      this.form.page = 1;
      this.form.limit = val;
      this.getList({
        pageNow: this.form.page,
        pageSize: this.form.limit,
        Gift_Card_Scheme_Name: ''
      })
    },
    //号码按键查询
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList({
        pageNow: this.form.page,
        pageSize: this.form.limit,
        Gift_Card_Scheme_Name: ''
      })
    },
    onSerach() {				//查询方法
      console.log("搜搜")
      this.form.page = 1;
      this.getList({
        pageNow: this.form.page,
        pageSize: this.form.limit,
        Gift_Card_Scheme_Name: this.form.serachKey})
    },
    addContract() {
      console.log("跳转")
    },
    getList(data) {     //换页方法
      console.log("换页",data)
      let _this = this
      $.ajax({
        type: 'GET',
        url: BASE_URL + '/GiftCard/getGiftCardList',
        data: data,
        // --------------------------------------------
        // type: 'POST',
        // url: DomainName + "/getprepaymentCardSchemeAction?token="+$.cookie('token'),
        // data,
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (Result) {
          console.log('Result', Result);
          _this.tableData =[]
          Result.data.data.forEach(item => {
              _this.tableData.push(item);
          });
          _this.total = Result.data.count;
               //将获取的数据存到表格数据中
        },
        dataType: "json"
      });
    },
    async downloadFile() {
      try {
        const response = await fetch('/your-endpoint/download', {
          method: 'GET',
          headers: {
            // 根据实际情况添加必要的请求头，这里可能不需要
          },
        });

        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }

        // 处理响应，触发文件下载
        const blob = await response.blob(); // 获取Blob对象
        const fileName = response.headers.get('content-disposition').split('filename=')[1].replace(/"/g, ''); // 获取文件名
        const url = window.URL.createObjectURL(blob); // 创建URL
        const a = document.createElement('a'); // 创建隐藏的可下载链接
        a.style.display = 'none';
        a.href = url;
        a.download = fileName;
        document.body.appendChild(a);
        a.click(); // 触发点击
        window.URL.revokeObjectURL(url); // 释放URL对象
        a.remove(); // 移除隐藏的链接
      } catch (e) {
        console.error('Download error:', e);
      }
    },
    //   30
    //   31// 调用函数下载文件
    //   32downloadFile();
    //   这段前端代码会发送一个GET请求到指定的下载端点，接收响应并将其转换为Blob对象，然后创建一个隐藏的<a>标签来触发文件的下载。请注意，实际使用时需要根据你的后端接口路径和实际情况调整代码。
    async Export(row) {
      try {
        const response = await fetch(BASE_URL+'/GiftCard/downloadZip?Gift_Card_Scheme_SerialNumber='+row.Giftt_Card_Scheme_SerialNumber);
        console.log(response);
        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }
        // 处理响应，触发文件下载
        const blob = await response.blob(); // 获取Blob对象
        console.log(blob,'1111')
        console.log(response.headers,'1111')
        // const fileName = response.headers.get('content-disposition').split('filename=')[1].replace(/"/g, ''); // 获取文件名
        const url = window.URL.createObjectURL(blob); // 创建URL
        const a = document.createElement('a'); // 创建隐藏的可下载链接
        a.style.display = 'none';
        a.href = url;
        a.download = row.Giftt_Card_Scheme_SerialNumber+'.zip';
        document.body.appendChild(a);
        a.click(); // 触发点击
        window.URL.revokeObjectURL(url); // 释放URL对象
        a.remove(); // 移除隐藏的链接
      } catch (e) {
        console.log('Download error:', e);
      }
      // console.log(row);
      // let _this = this;
      // $.ajax({
      //   type: 'GET',
      //   url: BASE_URL + '/GiftCard/downloadZip',
      //   data: {
      //     Gift_Card_Scheme_SerialNumber: row.Giftt_Card_Scheme_SerialNumber // 修正参数名
      //   },
      //   success: async function (response) {
      //     try {
      //       const blob = new Blob([response]); // 创建Blob对象
      //       const fileName = 'download.zip'; // 可以根据后端返回的文件名进行修改
      //       const url = window.URL.createObjectURL(blob); // 创建URL
      //       const a = document.createElement('a'); // 创建隐藏的可下载链接
      //       a.style.display = 'none';
      //       a.href = url;
      //       a.download = fileName;
      //       document.body.appendChild(a);
      //       a.click(); // 触发点击
      //       window.URL.revokeObjectURL(url); // 释放URL对象
      //       a.remove(); // 移除隐藏的链接
      //       _this.$message({ message: '导出成功', type: 'success' }); // 显示导出成功消息
      //     } catch (error) {
      //       console.error('导出错误:', error); // 捕获并打印任何错误
      //       _this.$message.error('导出失败，请重试'); // 显示导出失败消息
      //     }
      //   },
      //   error: function () {
      //     _this.$message.error('导出失败，请重试'); // 显示导出失败消息
      //   },
      //   dataType: "blob" // 声明预期的数据类型为blob
      // });
    }
    ,


    Delete(row) {
      console.log('Giftt_Card_Scheme_SerialNumber',row.Giftt_Card_Scheme_SerialNumber)
      console.log('1111',$.cookie('token'))
      const _this = this;
      $.ajax({
        type: 'POST',
        url: DomainName + "/DeleteAction?token=" + $.cookie('token'),
        data: {
          TableName: 'gift_card_scheme',
          Primary_Key: row.Giftt_Card_Scheme_SerialNumber,
          Primary_Key_Name: 'Giftt_Card_Scheme_SerialNumber',
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          // if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
          //   window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          // }
        },
        success: function (Result) {
          console.log('Result', Result);
          if (Result.Status == 'success') {
            _this.$message({
              message: '删除成功',
              type: 'success'
            });//将获取的数据存到表格数据中
            _this.getList({
              pageNow: _this.form.page,
              pageSize: _this.form.limit,
              Gift_Card_Scheme_Name: ''
            })
          }
        },
        dataType: "json"
      });
    },
    addEnterprise() {
      window.location.href = './Add.html'
    },
    showMember(EnterpriseInfo_SerialNumber) {
      sessionStorage.setItem("EnterpriseInfo_SerialNumber", EnterpriseInfo_SerialNumber)
      window.location.href = './show.html?EnterpriseInfo_SerialNumber=' + EnterpriseInfo_SerialNumber
    },
    editMember(EnterpriseInfo_SerialNumber) {
      sessionStorage.setItem("EnterpriseInfo_SerialNumber", EnterpriseInfo_SerialNumber)
      window.location.href = './Edit.html?EnterpriseInfo_SerialNumber=' + EnterpriseInfo_SerialNumber
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  },
})